import { ref, onMounted } from 'vue';
import { getRdsUrl, rdsRequestAsync } from './rdsServices';

export function usePayCodeHook() {
    const rdsUrl = getRdsUrl();

    const payClassItems = ref([]);
    const payCodeItems = ref([]);
    const payModeItems = ref([]);
    const paySceneItems = ref([]);
    const payRoundModeItems = ref([]);

    const initPayCodes = async () => {
        // const bcService = useService({ prefix: "rds-bc" });
        // const payClass = await bcService.request<string, any[]>('Rds公共服务', 'GetAllPayClass', '');
        const payClass = await rdsRequestAsync(rdsUrl.bcApi, 'Rds公共服务', 'GetAllPayClass', '');
        const payCode = await rdsRequestAsync(rdsUrl.bcApi, 'Rds公共服务', 'GetAllPayCode', '');
        const payMode = await rdsRequestAsync(rdsUrl.bcApi, 'Rds公共服务', 'GetAllPayMode', '');
        const payScene = await rdsRequestAsync(rdsUrl.bcApi, 'Rds公共服务', 'GetAllPayScene', '');
        const payRoundMode = await rdsRequestAsync(rdsUrl.bcApi, 'Rds公共服务', 'GetAllPayRoundMode', '');

        const [classResult, codeResult, modeResult, sceneResult, roundModeResult] = await Promise.all([payClass, payCode, payMode, payScene, payRoundMode]);

        if (!classResult.HasException) {
            payClassItems.value = classResult.ObjectData.map(item => ({
                value: item.c_pay_class,
                title: item.c_name
            }));
        }
        if (!codeResult.HasException) {
            payCodeItems.value = codeResult.ObjectData.map(item => ({
                value: item.c_pay_code,
                title: `[${item.c_pay_code}]${item.c_name}`,
                class: item.c_pay_class,
                name: item.c_name
            }));
        }
        if (!modeResult.HasException) {
            payModeItems.value = modeResult.ObjectData.map(item => ({
                value: item.c_pay_mode,
                title: item.c_name
            }));
        }
        if (!sceneResult.HasException) {
            paySceneItems.value = sceneResult.ObjectData.map(item => ({
                value: item.c_pay_scene,
                title: item.c_name
            }));
        }
        if (!roundModeResult.HasException) {
            payRoundModeItems.value = roundModeResult.ObjectData.map(item => ({
                value: item.c_code,
                title: item.c_name
            }));
        }
    };

    onMounted(async () => {
        await initPayCodes();
    });

    return {
        payCodeItems: payCodeItems,
        payClassItems: payClassItems,
        payModeItems: payModeItems,
        paySceneItems: paySceneItems,
        payRoundModeItems: payRoundModeItems,
    };
}
